package site.cege;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Rectangle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import site.cege.config.CellConfig;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ParsePDFJSONUtil {

    static Logger logger = LoggerFactory.getLogger(ParsePDFJSONUtil.class);


    public static JSONObject parseJsonFile(String filePath) throws IOException {
        checkFile(filePath);
        JSONObject result;
        try(FileInputStream fileInputStream = new FileInputStream(filePath)){
            result = JSON.parseObject(fileInputStream, JSONObject.class);
        }
        return result;
    }

    public static CellConfig parseCellConfig(JSONObject tableConfig){

        JSONObject cellJson = tableConfig.getJSONObject("cell");

        CellConfig cellConfig = parseConfig(cellJson);

        //获取列信息
        JSONArray columns = cellJson.getJSONArray("columns");
        cellConfig.setColumns(columns);

        return cellConfig;
    }

    public static CellConfig parseConfig(JSONObject masterConfig){
        JSONObject cellConfig = masterConfig.getJSONObject("config");

        //解析列的样式
        JSONObject cellBorderConfig = cellConfig.getJSONObject("border");
        int border = cellBorderConfig.getIntValue("enabled") == 0 ? Rectangle.NO_BORDER : Rectangle.BOX; //是否含有边框
        JSONObject borderColorConfig = cellBorderConfig.getJSONObject("color"); //边框颜色
        BaseColor borderColor = new BaseColor(borderColorConfig.getFloatValue("red"), borderColorConfig.getFloatValue("green"),borderColorConfig.getFloatValue("blue"), borderColorConfig.getFloatValue("alpha"));
        int verticalAlignment = cellConfig.getIntValue("verticalAlignment"); //内容垂直方式(居中，顶部)
        int horizontalAlignment = cellConfig.getIntValue("horizontalAlignment"); //内容水平方式(居中，顶部)
        int columnFixedHeight = cellConfig.getIntValue("fixedHeight"); //列高

        CellConfig cellConfigBean = new CellConfig();
        cellConfigBean.setBorder(border);
        cellConfigBean.setBorderColor(borderColor);
        cellConfigBean.setVerticalAlignment(verticalAlignment);
        cellConfigBean.setHorizontalAlignment(horizontalAlignment);
        cellConfigBean.setColumnFixedHeight(columnFixedHeight);

        return cellConfigBean;

    }

    public static void checkFile(String filePath) throws IOException {
        //Assert.checkNonNull(filePath, "Please enter json file path!");
        File jsonFile = new File(filePath);
        if(!jsonFile.exists()){
            logger.error("JSON file not found! [{}]", jsonFile.getAbsolutePath());
            throw new FileNotFoundException();
        }
        if(!jsonFile.canRead()){
            logger.error("JSON file not can read! [{}]", jsonFile.getAbsolutePath());
            throw new IOException("JSON file not can read!");
        }
    }

    public static Integer getColumns(JSONObject tableConfig){
        return tableConfig.getInteger("columns");
    }

    public static float[] getWidths(JSONObject tableConfig){
        JSONArray jsonArray = tableConfig.getJSONArray("widths");
        float[] widths = new float[jsonArray.size()];
        for (int i = 0; i < jsonArray.size(); i++) {
            widths[i] = jsonArray.getFloat(i);
        }
        return widths;
    }

    public static Float getWidthPercentage(JSONObject tableConfig) {
        return tableConfig.getFloat("widthPercentage");
    }

    public static Float getSpacingBefore(JSONObject tableConfig) {
        return tableConfig.getFloat("spacingBefore");
    }

}
